package at.htlkaindorf.jsf.view;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import at.htlkaindorf.jsf.application.DataBean;
import at.htlkaindorf.jsf.session.SessionBean;

@ManagedBean(name = "createBean")
@ViewScoped
public class CreateBean {

    @ManagedProperty(value = "#{sessionBean}")
    private SessionBean sessionBean;

    @ManagedProperty(value = "#{dataBean}")
    private DataBean dataBean;

    private String name, email, title, text;

    @PostConstruct
    public void init() {
        if (sessionBean.getUsername() == null) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("guestbookLogin.xhtml");
            } catch (IOException e) {
                // TODO: Logging of Java IOException.
            }
        }
        name = sessionBean.getUsername();
    }

    public String create() {


        dataBean.addGuestBookEntry(name, email, title, text);

        return "guestbookOverview";
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getEmail() {
        return email;
    }


    public void setEmail(String email) {
        this.email = email;
    }


    public String getTitle() {
        return title;
    }


    public void setTitle(String title) {
        this.title = title;
    }


    public String getText() {
        return text;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }


    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public void setText(String text) {
        this.text = text;
    }


    public DataBean getDataBean() {
        return dataBean;
    }


    public void setDataBean(DataBean dataBean) {
        this.dataBean = dataBean;
    }


}
